package com.dwxt.cdms.controller;

import com.dwxt.cdms.admin.service.UserServiceAdmin;
import com.dwxt.cdms.base.Page;
import com.dwxt.cdms.entity.Customer;
import com.dwxt.cdms.entity.dto.CustomerDTO;
import com.dwxt.cdms.service.CustomerService;
import com.dwxt.cdms.service.feign.FileService;
import com.dwxt.common.annotation.ValidateFiled;
import com.dwxt.common.annotation.ValidateGroup;
import com.dwxt.common.base.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;


/*
 * @Author liuhongfei
 * @Description 客户管理--后台
 * @Date 15:39 2020/9/10
 * @Param
 * @return
 */

@RestController
@RequestMapping("/cdms/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private UserServiceAdmin userServiceAdmin;

    /**
     * 新增/修改客户
     *
     * @param customer
     * @return
     */
    @RequestMapping("/insertOrUpdate")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, filedName = "name", notNull = true),
            @ValidateFiled(index = 0, filedName = "phone", notNull = true)}
    )
    public BaseResult insertOrUpdate(Customer customer) {
        return customerService.insertOrUpdate(customer);
    }


    @RequestMapping("/insertCustomerZT")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, filedName = "name", notNull = true),
            @ValidateFiled(index = 0, filedName = "phone", notNull = true)}
    )
    public BaseResult insertCustomerZT(Customer customer) {
        return customerService.insertCustomerZT(customer);
    }

    @Autowired
    private FileService fileService;

    /**
     * 删除客户
     *
     * @param ids
     * @return
     */
    @RequestMapping("/deleteByIds")
    public BaseResult deleteByIds(Integer[] ids) {
        return customerService.deleteByIds(ids);
    }

    /*
     * @Author liuhongfei
     * @Description 头像上传
     * @Date 9:52 2020/9/11
     * @Param
     * @return
     */
    @RequestMapping("/imageUpload")
    public BaseResult imageUpload(MultipartFile file) {
        return fileService.uploadHeadPicture(file);
    }

    /*
     * @Author liuhongfei
     * @Description  列表查询
     * @Date 17:02 2020/9/10
     * @Param
     * @return
     */
    @RequestMapping("/selectCustomerList")
    public BaseResult selectCustomerList(@RequestParam(required = true, defaultValue = "1") Integer pageNum,
                                         @RequestParam(required = true, defaultValue = "10") Integer pageSize,
                                         Customer customer, Integer minAge, Integer maxAge,
                                         @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                         @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                                         Integer expirationDays,Boolean isFiled) {
        return customerService.selectByQuery(pageNum, pageSize, customer, minAge, maxAge,startTime,endTime,expirationDays,isFiled);
    }

    @RequestMapping("/selectAllCustomerList")
    public BaseResult selectAllCustomerList(@RequestParam(required = true)String phone,
                                            @RequestParam(defaultValue = "1")Integer type) {
        return customerService.selectAllCustomerList(phone,type);
    }

    @RequestMapping("/selectCommunityCustomerList")
    public BaseResult selectCommunityCustomerList(@RequestParam(required = true, defaultValue = "1") Integer pageNum,
                                                  @RequestParam(required = true, defaultValue = "10") Integer pageSize,
                                                  String nameOrPhone, String phone) {
        return customerService.selectCommunityCustomerList(pageNum, pageSize, nameOrPhone, phone);
    }

    /*
     * @Author liuhongfei
     * @Description
     * @Date 15:34 2020/9/21
     * @Param
     * @return
     */
    @RequestMapping("/selectCustomerById")
    public BaseResult selectCustomerById(@RequestParam(required = true, name = "id") Integer id) {
        Customer customer = customerService.selectCustomerById(id);
        return BaseResult.ok(customer);
    }

    @RequestMapping("/selectWatchCustomer")
    public BaseResult selectWatchCustomer(Page page, Customer customer) {
        return customerService.selectWatchCustomer(page, customer);
    }

    /**
     * 添加管理人员
     */
    @RequestMapping("/addManagerPhone")
    public BaseResult addManagerPhone(Integer id, String phone) {
        return customerService.addManagerPhone(id, phone);
    }


    /**
     * 手动发送短信通知
     */
    @RequestMapping("/sendMessage")
    public BaseResult sendMessage(Integer id, String phone) {
        return customerService.sendMessage(id, phone);
    }

    @RequestMapping("/selectResponsibleList")
    public BaseResult selectResponsibleList(@RequestParam(required = true, name = "stationId") Integer stationId) {
        return userServiceAdmin.selectResponsibleList(stationId);
    }

    /**
     * @Description: 通过手机号码查询客户基本信息和健康数据
     * @param phone:
     * @return com.dwxt.common.base.BaseResult
     * @author chendq
     * @date 2021/8/20
     */
    @RequestMapping("/selectCustomerByPhone")
    public BaseResult selectCustomerByPhone(@RequestParam(required = true, name = "phone") String phone) throws ParseException {
        Customer customer = customerService.selectCustomerByPhone(phone);
        return BaseResult.ok(customer);
    }


    /**
     * @Description: 查询客户关联的医生营养师等(包含呼叫服务)
     * @param customerId:
     * @return com.dwxt.common.base.BaseResult
     * @author chendq
     * @date 2021/10/11
     */
    @RequestMapping("/selectCustomerRelationPeople")
    public BaseResult selectCustomerRelationPeople(@RequestParam(required = true, name = "customerId") Integer customerId)  {
        List<Map> list = customerService.selectCustomerRelationPeople(customerId);
        return BaseResult.ok(list);
    }

    /**
     * @Description: 查询客户关联的医生营养师等
     * @param customerId:
     * @return com.dwxt.common.base.BaseResult
     * @author chendq
     * @date 2021/10/18
     */
    @RequestMapping("/selectCustomerIndexRelationPeople")
    public BaseResult selectCustomerIndexRelationPeople(@RequestParam(required = true, name = "customerId") Integer customerId)  {
        List<Map> list = customerService.selectCustomerIndexRelationPeople(customerId);
        return BaseResult.ok(list);
    }



    @RequestMapping("/employeeAddCustomer")
    public BaseResult employeeAddCustomer(@RequestBody CustomerDTO customerDTO)  {
        return customerService.employeeAddCustomer(customerDTO);
    }


    /**
     * @Description: 用户签约
     * @Param [id]
     * @Return com.dwxt.common.base.BaseResult
     * @Author chendq
     * @Date 2021/12/31
     **/
    @RequestMapping("/privacy")
    public BaseResult privacy(Integer customerId,String sign){
        return customerService.privacy(customerId,sign);
    }


    @RequestMapping("/select72HourNoDataCustomerList")
    public BaseResult select72HourNoDataCustomerList(Page page){
        return customerService.select72HourNoDataCustomerList(page);
    }

    @RequestMapping("/updateSign")
    public BaseResult updateSign(@RequestParam(required = true)Integer customerId,@RequestParam(required = true)String signImage){
        return customerService.updateSign(customerId, signImage);
    }
}
